/* ===============================================================================
************   Address ************
=============================================================================== */
/* global $:true */
+function ($) {
    "use strict";
    var address, defaults = {
        addresslist: [],
        tpl: ' {{#addresslist}}<div data-addressid="{{this.AddressID}}" class="weui-media-box weui-media-box_text">\
                <h4 class="weui-media-box__title">{{this.Addressee}} | {{this.Tel}}\
                {{#if this.ischecked}}\
                    <label class="label f-white bg-green">默认</label>\
                {{/if}}\
                </h4>\
                <p class="weui-media-box__desc">{{this.AreaInfo}}</p>\
                </div>\
                {{/addresslist}}'
    }
    $.fn.lobsteraddress = function (params) {
        return this.each(function () {
            params.addresslist.forEach(a => {
                if (a.IsDefault == 1)
                    a.ischecked = true;
                else
                    a.ischecked = false;
            })
            params = $.extend({}, defaults, params);
            address = $(this);
            // address.off("address-click"); //the click event is not correct sometime: it will trigger when user is draging.
            // if (params.onClick) address.on("address-click", function () {
            //     params.onClick(params.data);
            // });
            address.html($.t7.compile(params.tpl)(params));
            $(this).on("click", ".weui-media-box_text", function () {
                var addressId = $(this).data("addressid")
                var item = params.addresslist.filter(a => { return a.AddressID == addressId });
                params.selectaddress && typeof params.selectaddress === 'function' && params.selectaddress(item[0]);
            })
        })
    };
}($);